// src/stores/user.ts
import { defineStore } from 'pinia';

interface UserState {
    user: null | {
        id: number;
        email: string;
        password: string;
        name: string;
        roleId: number;
        headTeacherId: number;
        counselorId: null | number;
        classId: null | number;
    };
}

export const useUserStore = defineStore('user', {
    state: (): UserState => ({
        user: null,
    }),
    actions: {
        setUser(user: {
            id: number;
            email: string;
            password: string;
            name: string;
            roleId: number;
            headTeacherId: number;
            counselorId: null | number;
            classId: null | number;
        }) {
            this.user = user;
            // 将用户信息存储到 localStorage
            localStorage.setItem('user', JSON.stringify(user));
        },
        loadUser() {
            // 从 localStorage 中加载用户信息
            const storedUser = localStorage.getItem('user');
            if (storedUser) {
                this.user = JSON.parse(storedUser);
            }
        },
        clearUser() {
            this.user = null;
            localStorage.removeItem('user'); // 清除 localStorage 中的用户信息
        },
    },
});
